package service

import (
	"liu_yu/config"
	"liu_yu/handler/request"
	"liu_yu/model"
	"liu_yu/pkg"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Register(c *gin.Context) {
	var form request.User
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数失败",
		})
		return
	}
	mobile := form.Phone
	result, _ := config.Rdb.Get(c, "sms"+mobile).Result()
	if result != form.Code {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证码错误",
		})
		return
	}
	var user model.User
	if err := user.CreatUser(config.DB, form.Phone); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "注册失败1",
		})
		return
	}
	if user.ID > 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "用户已存在",
		})
		return
	}
	user.Phone = form.Phone
	user.Password = pkg.MD5(form.Password)
	user.Username = form.Username
	if err := user.AddUser(config.DB); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "注册失败2",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
}
func Login(c *gin.Context) {
	var form request.Login
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数失败",
		})
		return
	}
	var user model.User
	if err := user.CreatUser(config.DB, form.Phone); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "登录失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.MD5(form.Password) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "密码错误",
		})
		return
	}
	token, _ := pkg.GetJwtToken(int(user.ID))
	c.JSON(http.StatusOK, gin.H{
		"code": 400,
		"msg":  "登录成功",
		"data": gin.H{
			"token": token,
		},
	})
}
func UsernameList(c *gin.Context) {
	var req request.UsernameList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数失败",
		})
		return
	}
	user, total, err := model.UsernameList(config.DB, req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "搜索失败",
			"err":  err.Error(),
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "搜索成功",
		"data": map[string]interface{}{
			"user":  user,
			"total": total,
		},
	})
}
func AddFriend(c *gin.Context) {
	var form request.AddFriend
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	var friend model.Friend
	if friend.IsFriend(config.DB, form.FriendID, form.UserID) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "好友已存在",
		})
		return
	}
	friend.FriendId = form.FriendID
	friend.UserId = form.UserID
	if err := friend.AddFriend(config.DB); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "添加好友失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加好友成功",
	})
}
func FriendList(c *gin.Context) {
	var form request.FriendList
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数失败",
		})
		return
	}
	var friend model.Friend
	if err := friend.FriendList(config.DB, form.FriendID); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"friend": friend,
		},
	})
}
func AddBlack(c *gin.Context) {
	userId, exists := c.Get("userId")
	if !exists {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "获取用户信息失败，请先登录",
		})
		return
	}
	userIdInt, ok := userId.(int)
	if !ok {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "用户id类型错误"})
		return
	}
	var req request.AddBlack
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	if err := model.AddBlacklist(int64(userIdInt), req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "拉黑成功"})
}
